package com.hmdp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmdp.dto.Result;
import com.hmdp.dto.UserDTO;
import com.hmdp.entity.Follow;
import com.hmdp.mapper.FollowMapper;
import com.hmdp.service.IFollowService;
import com.hmdp.service.IUserService;
import com.hmdp.utils.RedisConstants;
import com.hmdp.utils.UserHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 博主关注服务实现类
 *
 * @author leizi
 */
@Service
public class FollowServiceImpl extends ServiceImpl<FollowMapper, Follow> implements IFollowService {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private IUserService userService;

    @Override
    public Result isFollow(Long followId) {
        UserDTO user = UserHolder.getUser();
        Integer count = query().eq("follow_user_id", followId).eq("user_id", user.getId()).count();
        if (count > 0) {
            return Result.ok(true);
        }

        return Result.ok(false);
    }

    @Override
    public Result followUser(Long followId, boolean isFollow) {
        UserDTO user = UserHolder.getUser();
        // 关注
        if (isFollow) {
            Follow follow = new Follow();
            follow.setFollowUserId(followId);
            follow.setUserId(user.getId());
            boolean save = save(follow);
            if (save) {
                // 添加关注列表
                redisTemplate.opsForSet().add(RedisConstants.FOLLOW_USER_KEY + user.getId(), String.valueOf(followId));
            }
        } else {
            // 取消关注
            boolean isSuccess = remove(new QueryWrapper<Follow>()
                    .eq("user_id", user.getId())
                    .eq("follow_user_id", followId));
            if (isSuccess) {
                // 取消关注列表用户
                redisTemplate.opsForSet().remove(RedisConstants.FOLLOW_USER_KEY + user.getId(), String.valueOf(followId));
            }
        }
        return Result.ok();
    }

    @Override
    public Result followCommon(Long userId) {
        UserDTO user = UserHolder.getUser();
        Long currentLoginUserId = user.getId();

        // 被访者关注列表与当前登录用户关注列表取交集获取共同关注用户id列表
        Set<String> commonFollowIds = redisTemplate.opsForSet().intersect(RedisConstants.FOLLOW_USER_KEY + currentLoginUserId, RedisConstants.FOLLOW_USER_KEY + userId);
        if (CollectionUtils.isEmpty(commonFollowIds)) {
            return Result.ok();
        }
        // 根据共同关注用户id获取用户信息
        List<Long> userIds = commonFollowIds.stream().map(Long::valueOf).collect(Collectors.toList());
        List<UserDTO> userDTOList = userService.listByIds(userIds).stream().map(followUser -> {
            return BeanUtil.copyProperties(followUser, UserDTO.class);
        }).collect(Collectors.toList());

        return Result.ok(userDTOList);
    }


}
